package com.example.yoho.mvp.presenter;

import com.example.lib_core.mvp.presenter.BasePresenter;
import com.example.yoho.mvp.contract.AddCarContract;
import com.example.yoho.mvp.model.entity.AddcarEntity;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class AddCarPresenter extends BasePresenter<AddCarContract.Model, AddCarContract.View> {

    @Inject
    public AddCarPresenter() {
    }
    int userid=1;
    int goodsid=1;
    String shopname;
    String shopcolor;
    String shopsize;
    String shopnum;
    String shopprice;

    public void AddCar(int userid,int goodsid,String shopname,String shopcolor,String shopsize,String shopnum,String shopprice){
        this.userid=userid;
        this.goodsid=goodsid;
        this.shopname=shopname;
        this.shopcolor=shopcolor;
        this.shopsize=shopsize;
        this.shopnum=shopnum;
        this.shopprice=shopprice;

        JSONObject addcarJob = new JSONObject();
        try {
            addcarJob.put("userid",userid);
            addcarJob.put("goodsid",goodsid);
            addcarJob.put("shopname",shopname);
            addcarJob.put("shopcolor",shopcolor);
            addcarJob.put("shopsize",shopsize);
            addcarJob.put("shopnum",shopnum);
            addcarJob.put("shopprice",shopprice);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        mModel.addcarModel(addcarJob.toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AddcarEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposables.add(d);
                        mView.showDialog();
                    }

                    @Override
                    public void onNext(AddcarEntity addcarEntity) {
                        mView.showAddCar(addcarEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showToast("请求失败"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.hideDialog();
                    }
                });
    }
}
